package com.wandian.weather.mvvm.othercity.citychild;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.LocalCityBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.eventbus.ChangeMessage;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.CityDataUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.adapter.WeatherCityRecycleAdapter;
import com.wandian.weather.adapter.WeatherCityStreeRecycleAdapter;
import com.wandian.weather.databinding.WeatherActivityCityChildBinding;
import com.wandian.weather.mvvm.othercity.WeatherOtherCityActivity;

import org.greenrobot.eventbus.EventBus;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 地级市
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_CITY_CHILD)
public class WeatherCityChildActivity extends BaseMVVMActivity<WeatherCityChildViewModel> {
    private WeatherActivityCityChildBinding dataBinding;
    private WeatherCityRecycleAdapter weatherCityRecycleAdapter;
    private WeatherCityStreeRecycleAdapter weatherCityStreeRecycleAdapter;
    private String cityName;
    private String cityCode;
    private String cityChildName="";
    private String cityStreetName="";
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_city_child);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initClink();
        initViewData();
        initRecycle();
        dataBinding.recycler.post(new Runnable() {
             @Override
             public void run() {
                 CityDataUtils.getLocalCityJson(WeatherCityChildActivity.this);
             }
         });

    }

    private void initRecycle(){
        //市区
        weatherCityRecycleAdapter = new WeatherCityRecycleAdapter(R.layout.weather_item_city);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,3);
        dataBinding.recycler.setLayoutManager(layoutManager);
        dataBinding.recycler.setAdapter(weatherCityRecycleAdapter);
        CityDataUtils.initCityList(cityCode);
        weatherCityRecycleAdapter.setNewData(CityDataUtils.getListCity());
        weatherCityRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                LocalCityBean cityBean = (LocalCityBean)baseQuickAdapter.getData().get(i);
                modifySelectCity(cityBean);
            }
        });

        //区县
        weatherCityStreeRecycleAdapter = new WeatherCityStreeRecycleAdapter(R.layout.weather_item_city_street);
        RecyclerView.LayoutManager layoutManagerChild = new LinearLayoutManager(this);
        dataBinding.recyclerChild.setLayoutManager(layoutManagerChild);
        dataBinding.recyclerChild.setAdapter(weatherCityStreeRecycleAdapter);
        weatherCityStreeRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                LocalCityBean cityBean = (LocalCityBean)baseQuickAdapter.getData().get(i);
                modifySelectStreet(cityBean,i);
            }
        });

        //初始化选中数据
        LocalCityBean cityBean = (LocalCityBean)weatherCityRecycleAdapter.getData().get(0);
        modifySelectCity(cityBean);
    }

    //修改选中城市
    private void modifySelectCity(LocalCityBean cityBean){
        dataBinding.tvQxChange.setVisibility(View.GONE);
        dataBinding.tvCityChildStreet.setText("-");
        if(!UIUtils.isEmpty(cityBean)){
            weatherCityRecycleAdapter.notifySelectItemView(cityBean.getName());
            CityDataUtils.initStreetList(cityBean.getCode());
            cityChildName = cityBean.getName();
            dataBinding.tvCityChildName.setText(cityName+"-"+cityChildName+"");
            if(!UIUtils.isEmpty(CityDataUtils.getListStreet()) && CityDataUtils.getListStreet().size()>0){
                weatherCityStreeRecycleAdapter.setNewData(CityDataUtils.getListStreet());
                LocalCityBean streetBean = (LocalCityBean)weatherCityStreeRecycleAdapter.getData().get(0);
                modifySelectStreet(streetBean,0);
            }
            dataBinding.bindTransparentToolBar.toolBarTitleRight.setVisibility(View.VISIBLE);
//                    dataBinding.tvSqChange.setVisibility(View.VISIBLE);

        }
    }

    //修改选中区县
    private void modifySelectStreet(LocalCityBean cityBean,int index){
        dataBinding.tvQxChange.setVisibility(View.VISIBLE);
        if(!UIUtils.isEmpty(cityBean)){
            cityStreetName = cityBean.getName();
            dataBinding.tvCityChildStreet.setText(""+cityBean.getName());
        }
        weatherCityStreeRecycleAdapter.notifySelectItemView(index);
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        Bundle bundle = getIntent().getExtras();
       if(bundle!=null){
           cityName = bundle.getString("cityName")+"";
           cityCode = bundle.getString("cityCode")+"";
       }
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText(cityName+"天气");
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setText("切换");
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v ->finish());
        //切换
        dataBinding.tvQxChange.setOnClickListener(v -> getAddressWeather());
//        dataBinding.tvSqChange.setOnClickListener(v -> getAddressWeather());
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setOnClickListener(v -> getAddressWeather());
    }

    private void getAddressWeather(){
        String address = cityName+cityChildName+cityStreetName;
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_city",cityName);
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_address",address);
        EventBus.getDefault().post(new ChangeMessage("changeWeatherCity"));
        ActivityUtil.getInstance().finishActivity(WeatherOtherCityActivity.class);
        finish();
    }


}
